草庐IT

javascript - 在javascript中获取变量名到字符串中

全部标签

ruby - 使用正则表达式获取 URL 的域

我正在尝试获取给定URL的域。例如http://www.facebook.com/someuser/将返回facebook.com。给定的URL可以是以下格式:https://www.facebook.com/someuser(www.是可选的,但应忽略)www.facebook.com/someuser(http://不是必需的)facebook.com/someuserhttp://someuser.tumblr.com->这只能返回tumblr.com我写了这个正则表达式:/(?:\.|\/{2})(?:www\.)?([^\/]*)/i但它并没有像我预期的那样工作。我可以分部分

ruby - 给定一个 Ruby 元类,我如何获取它所附加的实例?

这是问题“GivenaninstanceofaRubyobject,howdoIgetitsmetaclass?”的反面您可以在默认的to_s输出中看到附加元类或单例类的对象的表示:s="hello"s_meta=class"#>"classC;endc_meta=class"#"是否有可能实现一个方法Class.attached来返回这个对象(如果接收者是一个普通类,则返回nil)?s_meta.attached#=>sc_meta.attached#=>CC.attached#=>nil 最佳答案 有一个丑陋的(但有效的)黑客,

ruby - 将多个变量与单个表达式中的值进行比较

我有两个变量a和b。我想将a和b都与一个值进行比较,例如10。我可以这样做:10==a&&10==b但是,我想知道是否有任何方法可以将它写成一个表达式?(例如像a==b==10) 最佳答案 [a,b,3].all?{|x|x==10}但在这种情况下[].all?{|x|x==10}也会返回true 关于ruby-将多个变量与单个表达式中的值进行比较,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/qu

ruby-on-rails - 来自带有参数的字符串的路径

我需要一种从数组创建一组Rails3路径的好方法,在link_to帮助程序中。我有:TITLES=['foo','bar','baz']TITLES.eachdo|t|=link_tot,(.....path....)这样我需要构建一组路径:foo_super_users_path(user)bar_super_users_path(user)baz_super_users_path(user)如您所见,我需要为每个路径添加相同的前缀_super_users,并传递user对象。作为最终结果,我需要类似的东西:link_tot,foo_super_users_path(user)lin

ruby - 如何从模块访问类变量?

我想知道如何从模块访问类变量moduleEntitydeffoo#puts@@rulesendendclassPersonincludeEntityattr_accessor:id,:name@@rules=[[:id,:int,:not_null],[:name,:string,:not_null]]endclassCarincludeEntityattr_accessor:id,:year@@rules=[[:id,:string,:not_null],[:year:,:int,:not_null]]endp=Person.newc=Car.newp.foo#[[:id,:int,

ruby - 任何人都知道字符串开头的 "weird"字符是怎么回事?

我在尝试从它们的数组中检测某个字符串时遇到了一个奇怪的问题。有人知道这里发生了什么吗?(rdb:1)pmagic_string"TimePeriod"(rdb:1)pmagic_string.classString(rdb:1)pmagic_string=="TimePeriod"false(rdb:1)p"TimePeriod".length11(rdb:1)pmagic_string.length14(rdb:1)pmagic_string[0].chr"\357"(rdb:1)pmagic_string[1].chr"\273"(rdb:1)pmagic_string[2].c

Ruby URI - 如何在 URL 之后获取完整路径

给定以下内容,如何获取URL的完整路径uri=URI("http://foo.com/posts?id=30&limit=5#time=1305298413")我只想要posts?id=30&limit=5#time=1305298413我试过uri.path并返回/posts和ui.query返回'id=30&limit=5' 最佳答案 您要找的方法是request_uriuri.request_uri=>"/posts?id=30&limit=5"如果需要,您可以使用任何您想要删除前导/的方法。编辑:要获取#符号后的部分,请使用

ruby - 将散列与 ruby​​ 中字符串的键/值合并

我正在尝试将散列与ruby​​中字符串的键/值合并。即h={:day=>4,:month=>8,:year=>2010}s="/my/crazy/url/:day/:month/:year"putss.interpolate(h)我所发现的只是迭代键并替换值。但是我不确定是否有更好的方法来做到这一点?:)classString definterpolate(e)  selfife.each{|k,v|self.gsub!(":#{k}","#{v}")} endend谢谢 最佳答案 无需重新发明Ruby内置函数:h={:day=>4

ruby - 将变量传递给 Liquid 模板中的模型实例方法

这个周末我一直在研究Liquid模板引擎,我想知道以下是否可行。假设我在Blog模型中有一个latest_posts方法,我可以将一个整数传递给该方法以获取最新的N篇文章。是否可以在液体模板中使用该方法?例如:classBloghas_many:postsdeflatest_posts(n)posts.latest(n)#usinganamedscopeenddefto_liquid(*args){'all_posts'=>posts.all,#allowsmetouse{%forpostsinblog.all_posts%}'last_post'=>post.last,#allows

ruby-on-rails - 获取当前季度的月数

我需要一个包含当前季度月份数字的数组。我想提供Date.today然后得到例如。[1,2,3]。如何以最简单的方式做到这一点?(不使用switch/case)。 最佳答案 defquarter(date)1+((date.month-1)/3).to_iend 关于ruby-on-rails-获取当前季度的月数,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/6322168/